﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UCNKompetenceBors.Login
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (!String.IsNullOrEmpty(culture))
                GeneralTools.SetCulture(culture);

            LocalizeControls();
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            System.Web.UI.WebControls.Login loginControl = (System.Web.UI.WebControls.Login)LoginView1.FindControl("LoginControl");

            if (this.Page.User.Identity.IsAuthenticated)
            {
                Label alreadyLoggedIn = (Label)LoginView1.FindControl("AlreadyLoggedInLabel");
                alreadyLoggedIn.Text = GetLocalResourceObject("lblAlreadyLoggedInText").ToString();
            }
            else
            {
                Button loginButton = (Button)loginControl.FindControl("LoginButton");
                loginButton.Text = GetLocalResourceObject("LoginButtonText").ToString();
                this.Form.DefaultButton = loginButton.UniqueID;

                RequiredFieldValidator rfvalPassword = (RequiredFieldValidator)loginControl.FindControl("PasswordRequired");
                string passwordValidate = GetLocalResourceObject("PasswordRequiredText").ToString();
                rfvalPassword.ForeColor = System.Drawing.Color.Red;
                rfvalPassword.Text = "*";
                rfvalPassword.ToolTip = passwordValidate;
                rfvalPassword.ErrorMessage = passwordValidate;

                CheckBox chkBoxRememberMe = (CheckBox)loginControl.FindControl("RememberMe");
                chkBoxRememberMe.Text = GetLocalResourceObject("RememberMeText").ToString();

                Label userNameLabel = (Label)loginControl.FindControl("UserNameLabel");
                userNameLabel.Text = GetLocalResourceObject("UserNameLabelText").ToString();

                RequiredFieldValidator rfvalUserName = (RequiredFieldValidator)loginControl.FindControl("UserNameRequired");
                string userNameValidate = GetLocalResourceObject("UserNameRequiredText").ToString();
                rfvalUserName.ForeColor = System.Drawing.Color.Red;
                rfvalUserName.Text = "*";
                rfvalUserName.ToolTip = userNameValidate;
                rfvalUserName.ErrorMessage = userNameValidate;

                HyperLink hyLinkRecoverPassword = (HyperLink)loginControl.FindControl("hyLinkRecoverPassword");
                hyLinkRecoverPassword.Text = GetLocalResourceObject("hyLinkRecoverPasswordText").ToString();

                Label titleLabel = (Label)loginControl.FindControl("TitleLabel");
                titleLabel.Text = GetLocalResourceObject("TitleLabelText").ToString();
            }
        }

        //Event fired when a login error happens 
        protected void LoginControl_LoginError(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Login loginControl = (System.Web.UI.WebControls.Login)sender;

            loginControl.FailureText = GetLocalResourceObject("FailureTextText").ToString();
        }
    }
}